﻿using Apewer.Web;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Apewer.AspNetBridge
{

    /// <summary></summary>
    public class HttpResponseMessage
    {

        /// <summary></summary>
        public HttpContent Content { get; set; }

        /// <summary></summary>
        public HttpStatusCode StatusCode { get; set; }

        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public HttpResponseMessage(HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            var code = (int)StatusCode;
            if (code < 0 || code > 999) throw new ArgumentOutOfRangeException("statusCode");
            StatusCode = statusCode;
        }

        /// <summary></summary>
        public override string ToString() => "";

        /// <summary></summary>
        internal ApiModel ToModel()
        {
            var hrm = this;
            if (hrm == null) return new ApiStatusModel(204);
            if (hrm.Content == null) return new ApiStatusModel(204);

            var stream = hrm.Content.Stream;
            if (stream == null) return new ApiStatusModel(204);

            var model = new ApiStreamModel(stream);
            model.Status = (int)hrm.StatusCode;
            if (hrm.Content.Headers != null)
            {
                if (hrm.Content.Headers.ContentType != null) model.ContentType = hrm.Content.Headers.ContentType.MediaType;
                if (hrm.Content.Headers.ContentDisposition != null) model.Attachment = hrm.Content.Headers.ContentDisposition.FileName;
            }
            return model;
        }

    }

}
